import axios from 'axios'
// 引入axios
//- 接口基准地址：`http://ludir.cn:8888/api/private/v1/`
// - 服务端已开启 CORS 跨域支持
// - API V1 认证统一使用 Token 认证
// - 需要授权的 API ，必须在请求头中使用 `Authorization` 字段提供 `token` 令牌
// - 使用 HTTP Status Code 标识状态
// - 数据返回格式统一使用 JSON 

// 设置基准路径
let baseUrl = 'http://ludir.cn:8888/api/private/v1/'

axios.defaults.baseURL = baseUrl;

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';


// 请求拦截
let AUTH_TOKEN = window.localStorage.getItem('myToken') || ''

axios.interceptors.request.use(function(config) {

    if (config.url.includes('1106975712662')) {
        config.baseURL = 'https://www.liulongbin.top:8888/api/private/v1/'
        config.headers['Authorization'] = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2Mzg5MjU4MDUsImV4cCI6MTYzOTAxMjIwNX0.R2S3WJp6i1DStWIKuDh8ffpEAbpOlNPt_DvJ-49MHZo'
    } else {
        config.baseURL = baseUrl
        config.headers['Authorization'] = AUTH_TOKEN; //在发送请求之前在请求头配置token
    }
    return config;

}, function(error) {
    return Promise.reject(error);
});

// 相应拦截
axios.interceptors.response.use(function(response) {
    return response.data;
}, function(error) {
    return Promise.reject(error)
})

// 封装统一api

// 登录请求
export let getLogin = (params) => axios.post('login', params)

// 获取users表
export let getUsers = (params) => axios.get('users', { params })

// 更改用户状态请求
export let getChangeUserState = (params = {}) => axios.put(`users/${params.id}/state/${params.type}`)

// 添加用户的请求
export let getAddUsers = (params) => axios.post('users', params)

// 删除用户请求
export let getDeleteUsers = (params) => axios.delete(`users/${params.id}`)

// 根据id查询单个用户请求
export let getQueryUsersById = (params) => axios.get(`users/${params.id}`)
    //编辑用户请求
export let getEditUsers = ({ id, mobile, email }) => axios.put(`users/${id}`, { mobile, email })

// 分配角色请求
export let getUsersRole = ({ id, rid }) => axios.put(`users/${id}/role`, { id, rid })

// 获取角色列表
export let getRoles = (params = {}) => axios.get(`roles`, { params })

// 获取左侧菜单权限请求 

export let getMenus = (params = {}) => axios.get('menus')

// 获取所有权限列表请求
export let getRights = (params) => axios.get(`rights/${params.type}`)

// 删除角色置顶权限
export let getDeleteRolesRights = ({ roleId, rightId }) => axios.delete(`roles/${roleId}/rights/${rightId}`)

// 角色授权
export let getRolesRights = ({ roleId, rids }) => axios.post(`roles/${roleId}/rights`, { rids })
    // 添加角色请求
export let getAddRoles = (params) => axios.post('roles', params)
    // 删除角色请求
export let getDeleteRoles = (params) => axios.delete(`roles/${params.id}`)
    // 通过单个id查询角色请求
export let getQueryRolesById = (params) => axios.get(`roles/${params.id}`)
    // 编辑角色请求
export let getEditRoles = ({ id, roleName, roleDesc }) => axios.put(`roles/${id}`, { roleName, roleDesc })
    // 获取图表请求
export let getEcharts = (params = {}) => axios.get(`reports/type/1`)

// 获取订单数据请求
export let getOrders = (params = {}) => axios.get(`orders`, { params })
    // 根据id查询订单
export let getList = (params = {}) => axios.get(`orders/${params.id}`)
    // 获取物流信息
export let getAddress = (params) => axios.get(`kuaidi/${params.id}`)